home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / El temporizador y la hora / SevenSegmentClock / SevenSegmentClock.cs next >
Encoding:
Text File  |  2002-07-15  |  2.1 KB  |  62 lines

  1. //------------------------------------------------
  2. // SevenSegmentClock.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.Windows.Forms;
  8. using Petzold.ProgrammingWindowsWithCSharp;
  9.  
  10. class SevenSegmentClock: Form
  11. {
  12.      DateTime dt;
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new SevenSegmentClock());
  17.      }
  18.      public SevenSegmentClock()
  19.      {
  20.           Text = "Reloj de siete segmentos";
  21.           BackColor = Color.White;
  22.           ResizeRedraw = true;
  23.           MinimumSize = SystemInformation.MinimumWindowSize + new Size(0, 1);
  24.  
  25.           dt = DateTime.Now;
  26.  
  27.           Timer timer    = new Timer();
  28.           timer.Tick    += new EventHandler(TimerOnTick);
  29.           timer.Interval = 100;
  30.           timer.Enabled  = true;
  31.      }
  32.      void TimerOnTick(object obj, EventArgs ea)
  33.      {
  34.           DateTime dtNow = DateTime.Now;
  35.           dtNow = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day,
  36.                                dtNow.Hour, dtNow.Minute, dtNow.Second);
  37.           if (dtNow != dt)
  38.           {
  39.                dt = dtNow;
  40.                Invalidate();
  41.           }
  42.      }
  43.      protected override void OnPaint(PaintEventArgs pea)
  44.      {
  45.           SevenSegmentDisplay ssd = new SevenSegmentDisplay(pea.Graphics);
  46.  
  47.           string strTime = dt.ToString("T", 
  48.                                        DateTimeFormatInfo.InvariantInfo);
  49.           SizeF  sizef   = ssd.MeasureString(strTime, Font);
  50.           float  fScale  = Math.Min(ClientSize.Width  / sizef.Width,
  51.                                     ClientSize.Height / sizef.Height);
  52.           Font   font    = new Font(Font.FontFamily, 
  53.                                     fScale * Font.SizeInPoints);
  54.  
  55.           sizef = ssd.MeasureString(strTime, font);
  56.  
  57.           ssd.DrawString(strTime, font, Brushes.Red, 
  58.                          (ClientSize.Width  - sizef.Width) / 2, 
  59.                          (ClientSize.Height - sizef.Height) / 2);
  60.      }
  61. }
  62.